package com.mpgame.core.loader;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mpgame.core.effect.DamageEffect;
import com.mpgame.core.effect.Effect;
import com.mpgame.core.effect.ReinforceEffect;
import com.mpgame.core.effect.StatusEffect;
import com.mpgame.core.model.Skill;
import com.mpgame.core.model.SkillType;
import com.mpgame.core.utils.FileHelper;

public class SkillLoader {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Skill loadSkill(int familyId, int power, int priority, int maxPp, double accuracy, SkillType skillType, String effectJsonPath){
        try{
            if(effectJsonPath == null || effectJsonPath.trim().isEmpty()){
                return new Skill(familyId, power, priority, maxPp, accuracy, skillType, new ArrayList<>(), new ArrayList<>());
            }
            String jsonContent = FileHelper.getFileContent("skills/" + effectJsonPath + ".json");
            if(jsonContent == null || jsonContent.trim().isEmpty()){
                System.out.println("未找到json文件！");
                return new Skill(familyId, power, priority, maxPp, accuracy, skillType, new ArrayList<>(), new ArrayList<>());
            }
            JsonNode rootNode = objectMapper.readTree(jsonContent);
            List<Effect> effect_to_self = new ArrayList<>();
            List<Effect> effect_to_opponent = new ArrayList<>();
            if(rootNode.has("effect_to_self")){
                JsonNode selfEffects = rootNode.get("effect_to_self");
                for(JsonNode effectNode : selfEffects){
                    Effect effect = parseEffect(effectNode);
                    if(effect != null) effect_to_self.add(effect);
                }
            }
            if(rootNode.has("effect_to_opponent")){
                JsonNode opponentEffects = rootNode.get("effect_to_opponent");
                for(JsonNode effectNode : opponentEffects){
                    Effect effect = parseEffect(effectNode);
                    if(effect != null) effect_to_opponent.add(effect);
                }
            }
            return new Skill(familyId, power, priority, maxPp, accuracy, skillType, effect_to_self, effect_to_opponent);
        }catch(JsonProcessingException e){
            System.out.println("Json处理失败：" + e.getMessage());
            return null;
        }
    }

    private static Effect parseEffect(JsonNode effectNode){
        String type = effectNode.get("type").asText();
        JsonNode params = effectNode.get("params");
        switch(type.toLowerCase()){
            case "damage":
                DamageEffect damageEffect = new DamageEffect();
                damageEffect.parseJson(params);
                return damageEffect;
            case "status":
                StatusEffect statusEffect = new StatusEffect();
                statusEffect.parseJson(params);
                return statusEffect;
            case "reinforce":
                ReinforceEffect reinforceEffect = new ReinforceEffect();
                reinforceEffect.parseJson(params);
                return reinforceEffect;
            default:
                throw new IllegalArgumentException("未知效果类型：" + type);
        }
    }
}
